import axios from 'axios';
import { message } from 'antd';
// import router from "@/router";
// axios.defaults.baseURL = 'http://47.106.186.202:8090/';
axios.defaults.baseURL = 'http://localhost.charlesproxy.com:3000/';

//拦截器
axios.interceptors.request.use(function(config) {
    // 在发送请求之前做些什么
    config.params = {
        ...config.params,
        // appkey: 'dongmeiqi_i_1545272862243'
    }
    config.headers['Accept'] = 'application/json';
    config.headers['Access-Control-Allow-Origin'] = '*';
    config.headers['Access-Control-Allow-Headers'] = 'X-Requested-With';
    config.headers['Access-Control-Allow-Methods'] = 'PUT,POST,GET,DELETE,OPTIONS';
    config.headers['X-Powered-By'] = '3.2.1';
    config.headers['Content-Type'] = 'application/json;';
    config.headers['token'] = localStorage.getItem("dake-token");
    return config
}, function(error) {
    // 对请求错误做些什么
    return Promise.reject(error)
})
axios.interceptors.response.use(res => {
    // console.log(res)
    // 对响应数据做些什么
    // console.log(res)
    // if(res.status == 200){
    //
    // }
    // console.log(res)
    // if (res.data.response.message == 'Unauthenticated.') {
    //     message.error('token过期，请重新登录');
    //     this.$router.push({
    //         path: `/login`,
    //     })
    // }
    return res
}, err => {
    // 对响应错误做些什么
    message.error("失败");
    if(err == 'Error: Request failed with status code 401'){
        message.error('token过期，请重新登录');
        // router.push({
        //     path: `/login`,
        // })
    }
    // return Promise.reject(err)
})

export default axios
